﻿using System;
using System.Collections.Generic;
using System.Linq;
using InvestmentIntelligence.Data.Repository.Comparers.EntityModelsComparers;
using InvestmentIntelligence.DbModel.Models;

namespace InvestmentIntelligence.Data.Repository.EFImplementation
{
    public class CurrencyRepository : IIPRepository<Currency>, ICurrencyUpdatableRepository
    {
   

        public List<T> GetList<T>(Func<IQueryable<Currency>, IEnumerable<T>> f, string[] code)
        {
            var codes = code.Select(x => x.ToUpper());
            return GetList(dc => f(dc.Where(x => codes.Contains(x.ISOCode))));
        }

        public Currency Get(int currncyId)
        {
            return base.Get(v => v.SingleOrDefault(i => i.Id == currncyId));
        }

        public Currency Get(string currencyCode)
        {
            return base.Get(v => v.SingleOrDefault(x => x.ISOCode.Equals(currencyCode, StringComparison.InvariantCultureIgnoreCase)));
        }

        public new int Add(Currency currency)
        {
           base.Add(currency);
            return currency.Id;
        }


        public List<Currency> SaveNew(IEnumerable<Currency> curr)
        {
            var currencies =GetList();
            var newCurrencies = curr.Except(currencies, new CurrencyCodeComparer());
            foreach (var curency in newCurrencies)
            {
                curency.ISOCode.ToUpper();
                
            }
            Add(newCurrencies);
            return GetList();
        }
    }
}